package com.atguigu.jucdemo.rwLock;

import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class LockDowngradingDemo {
    private ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
    private int data=0;
    public int readData() {
        readWriteLock.readLock().lock();
        try {
            return data;
        } finally {
            System.out.println("888");
            readWriteLock.readLock().unlock();
        }
    }

    public void updateData(int newData) {
        readWriteLock.writeLock().lock();
        try {
            data=newData;
            readWriteLock.readLock().lock();
        } finally {
            readWriteLock.writeLock().unlock();
        }
        int i = readData();
        System.out.println(i);
        readWriteLock.readLock().unlock();
    }

    public static void main(String[] args) {
        LockDowngradingDemo lockDowngradingDemo = new LockDowngradingDemo();
        lockDowngradingDemo.updateData(45);
    }
}
